package com.suke.czx.modules.wx.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

/**
 * 编码工具
 */
public class CodeUtil {

	/**
	 * 生成唯一订单号 https://blog.csdn.net/simplemurrina/article/details/60574815
	 * 
	 * @param machineId
	 * @return
	 */
	private static String genId(String machineId) {
		return machineId + (System.currentTimeMillis() + "").substring(1) + (System.nanoTime() + "").substring(7, 10);
	}

	/**
	 * 生成购买课程订单id
	 * 
	 * @return
	 */
	public static String createOrderNo() {
		return genId("gm_");
	}

	/**
	 * 生成补卡订单id
	 * 
	 * @return
	 */
	public static String createReplenOrderNo() {
		return genId("bk_");
	}

	/**
	 * 生成退款订单id
	 * 
	 * @return
	 */
	public static String createRefundNo() {
		return genId("tk_");
	}

	/**
	 * 获取ip地址
	 * 
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
		InetAddress addr = null;
		try {
			addr = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			return request.getRemoteAddr();
		}
		byte[] ipAddr = addr.getAddress();
		String ipAddrStr = "";
		for (int i = 0; i < ipAddr.length; i++) {
			if (i > 0) {
				ipAddrStr += ".";
			}
			ipAddrStr += ipAddr[i] & 0xFF;
		}
		return ipAddrStr;
	}
	public static String  randomStr(int i) {
		StringBuilder sb = new StringBuilder() ;
		for (int j = 0; j < i; j++) {
			//生成一个97-122之间的int类型整数--为了生成小写字母
			int intValL = (int)(Math.random()*26+97);
			//生成一个65-90之间的int类型整数--为了生成大写字母
			//int intValU = (int)(Math.random()*26+65);
			int intValU = (int)(Math.random()*26+97);;
			//生成一个30-39之间的int类型整数--为了生成数字
			//int intValN = (int)(Math.random()*10+48);
			int intValN = (int)(Math.random()*26+97);;
			
			int intVal = 0;
			int r = (int)(Math.random()*3);
			
			if(r==0) {
				intVal = intValL;
			}else if (r==1) {
				intVal = intValU;
			}else {
				intVal = intValN;
			}
			
			sb.append((char) intVal);
		}
		
		return sb.toString();
	}
}